import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../components/cq-layout/index.vue'
import { usePageStore } from '@/stores/page'
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', redirect: '/index' },
      {
        path: '/index',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '橙券首页'
        }
      },
      {
        path: '/news',
        component: () => import('@/views/news/index.vue'),
        meta: {
          title: '橙券新闻'
        }
      },
      {
        path: '/detail/:id',
        component: () => import('@/views/detail/index.vue'),
        meta: {
          title: '橙券新闻详情'
        }
      },
      {
        path: '/apply',
        component: () => import('@/views/apply/index.vue'),
        meta: {
          title: '加入我们'
        }
      },
      {
        path: '/case',
        component: () => import('@/views/case/index.vue'),
        meta: {
          title: '行业案例'
        }
      },
      {
        path: '/about',
        component: () => import('@/views/about/index.vue'),
        meta: {
          title: '关于我们'
        }
      }
    ]
  },
  {
    path: '/:catchAll(.*)',
    component: () => import('@/views/errPage/index.vue')
  }
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  console.log('全局前置守卫:beforeEach')
  const store = usePageStore()
  if (to?.meta?.title) {
    document.title = to.meta.title
  }
  store.setRouterPath(to.path)
  next()
})
export default router
